package test.utils;

import static org.testng.Assert.fail;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Logger;

public class GetProperties {
	
	private Properties props = new Properties();
	private static Logger logger = Logger.getLogger(GetProperties.class);

	public GetProperties() {
		
	}
	
	private static String getFilePath(String filename) {
		return getResourcesPath()+filename;
	}
	
	/**
	 * 获取资源文件路径
	 * @return
	 */
	public static String getResourcesPath() {
		String path = System.getProperty("catalina.home");
		if (path == null) {
			path = System.getProperty("user.dir");
			if (path.contains("target")) {
				path = path.replace("/target", "");
			}
			return path+"/src/main/resources/";
		}else {
			path = path+"/conf/";
			return path;
		}
		
	}
	
	public static GetProperties loadPrperties(String filename) {
		GetProperties getProperties = new GetProperties();
		FileInputStream is = null;
		String filePath = getFilePath(filename);
		try {
			is = new FileInputStream(filePath);
			getProperties.props.load(is);
	    } catch (IOException e) {
	    	logger.error("配置文件地址："+filePath);
	    	e.printStackTrace();
	    	fail("load properties file failure");
	    } finally {
	        if (is != null) {
	        	try {
	        		is.close();
	        		} catch (IOException e) {
	        			fail("close input stream failure");
	        			}
	        	}
	    }
		return getProperties;
	}
	
	public Enumeration<Object> getKeys() {
		return props.keys();
	}
	
	public GetProperties(String filename) {
		FileInputStream is = null;
		String filePath = getFilePath(filename);
		try {
			is = new FileInputStream(filePath);
	        props = new Properties();
	        props.load(is);
	    } catch (IOException e) {
	    	logger.error("配置文件地址："+filePath);
	    	e.printStackTrace();
	    	fail("load properties file failure");
	    } finally {
	        if (is != null) {
	        	try {
	        		is.close();
	        		} catch (IOException e) {
	        			fail("close input stream failure");
	        		}
	        	}
	        }
		}
	
	/**
     * 获取字符型属性（默认值为空字符串）
     */
    public String getString(String key) {
        return getString(key, "");
    }
    
    /**
     * 获取字符型属性（可指定默认值）
     */
    private String getString(String key, String defaultValue) {
        String value = defaultValue;
        if (props.containsKey(key)) {
            value = props.getProperty(key).trim().replace(";", "");
        }
        return value;
    }
    
    /**
     * 获取数值型属性（默认值为 0）
     */
    public int getInt(String key) {
        return getInt(key, 0);
    }

    /**
     * 获取数值型属性（可指定默认值）
     * @param key
     * @param defaultValue
     * @return
     */
    private int getInt(String key, int defaultValue) {
        int value = defaultValue;
        if (props.containsKey(key)) {
            value = Integer.valueOf(props.getProperty(key).trim().replace(";", ""));
        }
        return value;
    }
    
    /**
     * 获取布尔型属性（默认值为 false）
     */
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * 获取布尔型属性（可指定默认值）
     */
    private boolean getBoolean(String key, boolean defaultValue) {
        boolean value = false;
        if (props.containsKey(key)) {
            String aString = props.getProperty(key).trim().replace(";", "");
            if ("true".equals(aString.toLowerCase()) || "1".equals(aString)) {
				value = true;
			}
        }
        return value;
    }
}
